package com.example.springboot.service.Impl;

import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.springboot.entity.Post;
import com.example.springboot.entity.User;
import com.example.springboot.entity.UserEntity;
import com.example.springboot.mapper.CollectMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.JwtUtil;
import com.example.springboot.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CollectMapper collectMapper;
    private JwtUtil jwtUtil;
    @Override
    public boolean verify(UserEntity user) {
        String password=userMapper.getPassword(user);
        boolean flag=false;
        if(password!=null&&password.equals(user.getPassword())){
            flag=true;
        }
        return flag;
    }

    @Override
    public Integer getAuthorId(String tokens) {
        String username = null;
        if(tokens!=null){
            // 解析指定的token
            DecodedJWT decodedJWT = jwtUtil.decodeRsa(tokens);
            Claim userName = decodedJWT.getClaim("username");
            username=userName.asString();
        }
        Integer authorId = userMapper.findByUsername(username).getId();
        return authorId;
    }

    @Override
    public User getUserInfo(String token) {
        String username = null;
        if(token!=null){
            if(token.contains("Bearer ")){
                token=token.substring(7);
            }
            // 解析指定的token
            DecodedJWT decodedJWT = jwtUtil.decodeRsa(token);
            Claim userName = decodedJWT.getClaim("username");
            username=userName.asString();
        }
        User res= userMapper.findByUsername2(username);
        return res;
    }

    @Override
    public User getUserById(Integer authorId) {
        String key = "user:" + authorId;
        User user = null;
        try {
            user=(User) redisUtil.get(key);
        } catch (Exception e) {
            user = userMapper.findById(authorId);
            return user;
        }
        if(user==null){
            user = userMapper.findById(authorId);
            if (user != null) {
                // 将查询到的数据存入 Redis
                redisUtil.set(key, user);
                redisUtil.expire(key, 60 * 60 * 24);
            }
        }
        return user;
    }

    @Override
    public void updateUserInfo(User user) {
        userMapper.updateUserInfo(user);
    }

    @Override
    public User getUserByUsername(String username) {
        User res=userMapper.getUserByUsername(username);
        return res;
    }

    @Override
    public List<Post> getCollectPost(Integer id, Integer pageNum) {
        Integer offset=(pageNum-1)*10;
        List<Post> post=collectMapper.getCollectPostByUserId(id,offset);
        return post;
    }

    @Override
    public List<Post> getFollowPost(Integer id, Integer pageNum) {
        Integer offset=(pageNum-1)*10;
        List<Post> post=collectMapper.getFollowPostByUserId(id,offset);
        return post;
    }

    @Override
    public List<Post> getUserPost(Integer id, Integer pageNum) {
        Integer offset=(pageNum-1)*10;
        List<Post> post=collectMapper.getUserPostByUserId(id,offset);
        return post;
    }

    @Override
    public Integer getCollectPostNum(Integer id) {
        Integer res=collectMapper.getCollectPostNumByUserId(id);
        return res;
    }

    @Override
    public Integer getUserPostNum(Integer id) {
        Integer res=collectMapper.getUserPostNum(id);
        return res;
    }

    @Override
    public List<User> findAll() {
        List<User> users = userMapper.findAll();
        return users;
    }

    @Override
    public User selectUserByOpenId(User user) {
        User res = null;
        res = userMapper.selectUserByOpenId(user);
        if(res != null){
            return res;
        }else{

            userMapper.insert(user);
            res = user;
        }
        return res;
    }

    @Override
    public void changePassword(String username, Integer userId, String oldPassword, String newPassword) {
        UserEntity user = new UserEntity(username);
        String password = userMapper.getPassword(user);
        if(!password.equals(oldPassword)){
            throw new RuntimeException("密码错误");
        }
        userMapper.changePassword(userId,username,newPassword);
    }

    @Override
    public void insert(UserEntity user) {
        userMapper.register(user);
    }
}
